package com.goldsign.pay.provider.accountbase.service.impl;

import com.goldsign.pay.common.log.SysLog;
import com.goldsign.pay.common.util.Constant;
import com.goldsign.pay.provider.accountbase.service.AccountBaseService;
import com.goldsign.pay.provider.accountbase.service.AccountbaseApi;
import com.goldsign.pay.provider.accountbase.to.*;
import com.goldsign.pay.provider.accountbase.service.CommonService;
import com.goldsign.pay.provider.accountbase.vo.AccountInfoDO;
import com.goldsign.pay.common.exception.GcServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountbaseApiImpl implements AccountbaseApi {

	@Autowired
	AccountBaseService service;
	@Autowired
	private CommonService commonService;

	@Override
	public CreateAccountRespDTO createAccount(CreateAccountReqDTO req) {
		SysLog.info("收到  创建账户服务(createAccount) 请求:" + req);
		CreateAccountRespDTO resp = new CreateAccountRespDTO();

		try {
			// get user_no
			String accountNo = commonService.execGetWaterNo("t_account_info","account_no");

			service.createAccount(req, accountNo);

			resp.setAccount_no(accountNo);
			resp.setResp_code(Constant.SUCCESS);
			resp.setResp_msg("SUCCESS");

		} catch (GcServiceException e) {
			SysLog.error(e.getMessage());

			resp.setResp_code(e.getErrorCode());
			resp.setResp_msg(e.getMessage());
		} catch (Exception e) {
			SysLog.error(e.getMessage());

			resp.setResp_code(Constant.ACCOUNTBASE_SYSTERM_ERROR);
			resp.setResp_msg("ACCOUNTBASE_SYSTERM_ERROR");
		}
		SysLog.info("返回  创建账户服务(checkUserLoginPasswd) 响应:" + resp);
		return resp;
	}

	@Override
	public GetAccountInfoRespDTO getAccountInfo(GetAccountInfoReqDTO req) {
		SysLog.info("收到 获取账户信息服务(getAccountInfo)请求"+ req);
		
		GetAccountInfoRespDTO resp = new GetAccountInfoRespDTO();
		try {
			AccountInfoDO accountInfo = service.getAccountInfo(req.getAccount_no());
			
			resp.setResp_code(Constant.SUCCESS);
			resp.setResp_msg("SUCCESS");
			resp.setAccountInfo(accountInfo);
			
		} catch (GcServiceException e) {
			resp.setResp_code(e.getErrorCode());
		} catch (Exception e) {
			SysLog.error(req + "_"+ e.getMessage());
			resp.setResp_code(Constant.ACCOUNTBASE_SYSTERM_ERROR);
			resp.setResp_msg("ACCOUNTBASE_SYSTERM_ERROR");
		}

		SysLog.info("返回 获取账户信息服务(getAccountInfo) 响应"+ resp);
		return resp;
	}

	@Override
	public ListAccountInfoRespDTO listAccountInfo(ListAccountInfoReqDTO req) {
		SysLog.info("收到 获取账户信息列表服务(listAccountInfo)请求"+ req);
		
		ListAccountInfoRespDTO resp = new ListAccountInfoRespDTO();
		try {
			List<AccountInfoDO> accountInfoList = service.listAccountInfo(req.getUser_no());
			
			resp.setResp_code(Constant.SUCCESS);
			resp.setResp_msg("SUCCESS");
			resp.setAccountInfoList(accountInfoList);
			
		} catch (GcServiceException e) {
			resp.setResp_code(e.getErrorCode());
			resp.setResp_msg(e.getMessage());
		} catch (Exception e) {
			SysLog.error(req + "_"+ e.getMessage());
			resp.setResp_code(Constant.ACCOUNTBASE_SYSTERM_ERROR);
			resp.setResp_msg("ACCOUNTBASE_SYSTERM_ERROR");
		}

		SysLog.info("返回  获取账户信息列表服务(listAccountInfo) 响应"+ resp);
		return resp;
	}

}
